package regpang.myapplication22.musicplayer;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

import regpang.myapplication22.R;
import regpang.myapplication22.musicplayer.utils.LogUtil;
import regpang.myapplication22.musicplayer.utils.MusicPlay;

public class MusicPlayerActivity extends Activity implements View.OnClickListener {

    private final int MUSIC_FINISH = 1000;
    ImageView iv_bg_music;
    ImageView titlebar_back_music;
    TextView titlebar_title_music;
    //播放音乐
    CheckBox ib_paly_music;
    //播放音乐
    LinearLayout ll_to_play;
    //正在播放的时间
    TextView tv_time_progress;
    //播放总时间
    TextView tv_time_totle;
    //播放拖动条
    SeekBar sb_music_progress;
    private MusicPlay mausicPlay;
    private SimpleDateFormat formatter;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MUSIC_FINISH:
                    tv_time_progress.setText("0:00");//播放完毕后，重新设置没有播放的状态
                    sb_music_progress.setProgress(0);
                    ib_paly_music.setChecked(false);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_player);

        iv_bg_music = (ImageView) findViewById(R.id.iv_bg_music);
        titlebar_back_music = (ImageView) findViewById(R.id.titlebar_back_music);
        titlebar_title_music = (TextView) findViewById(R.id.titlebar_title_music);
        ib_paly_music = (CheckBox) findViewById(R.id.ib_paly_music);
        ll_to_play = (LinearLayout) findViewById(R.id.ll_to_play);
        tv_time_progress = (TextView) findViewById(R.id.tv_time_progress);
        tv_time_totle = (TextView) findViewById(R.id.tv_time_totle);
        sb_music_progress = (SeekBar) findViewById(R.id.sb_music_progress);

        initListener();
        initView();
    }

    private void initView() {
        formatter = new SimpleDateFormat("mm:ss");
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
        mausicPlay = MusicPlay.getInstance(this).setMySeekListener(sb_music_progress)
                .setMySeekProgressListener(new MusicPlay.MySeekProgressListener() {
                    @Override
                    public void onMySeekProgressData(String totleTime, String currentTime, int finishStatus) {
                        if (finishStatus == 100) {//播放完毕
                            LogUtil.e("yuyahao", "mian 时间 放放完啦！！！ ");
                        } else {
                            tv_time_totle.setText(totleTime);
                            if (totleTime.equals(currentTime)) {//用时间字符串进行比较，防止出现两次的时间结束状态
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        Message msg = handler.obtainMessage();
                                        msg.what = MUSIC_FINISH;
                                        handler.sendMessage(msg);
                                    }
                                }, 800);
                            } else {
                                tv_time_totle.setText(totleTime);
                                tv_time_progress.setText(currentTime);
                            }
                            // LogUtil.e("yuyahao","mian_    totle:  " +totleLong1+"当前：  " + currentLong1+"    "+totleLong+"   "+currentLong +  "    " +finishStatus);
                        }
                    }
                });
        ib_paly_music.setChecked(false);
    }

    private void initListener() {
        ib_paly_music.setOnClickListener(this);
        titlebar_back_music.setOnClickListener(this);
        ll_to_play.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_to_play:
            case R.id.ib_paly_music://博faNG或者是暂停
                Integer tag = (Integer) ib_paly_music.getTag();

                if (mausicPlay.isPlayIng()) {//正在播放，去暂停播放
                    ib_paly_music.setChecked(false);
                } else {//去进行播放
                    ib_paly_music.setChecked(true);
                }
                mausicPlay.play();
                break;
            case R.id.titlebar_back_music:
                onBackPressed();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mausicPlay != null) {
            mausicPlay.onDestroy();
        }
    }

    @Override
    public void onBackPressed() {
        if (mausicPlay.isPlayIng()) {
        } else {
            super.onBackPressed();
        }
    }
}
